import { Request, RequestHandler, Response } from 'express';
import {
  IGetResearchGroupMembersByIdReq,
  IAddResearchGroupMembersReq,
  IDeleteResearchGroupMembersReq
} from '../../models/research-group-members.model';
import * as ResearchGroupMembersService from '../services/research-group-members.service';

/**
 * Get active researchGroupMembers records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getResearchGroupMemberList: RequestHandler = async (req: Request, res: Response) => {
  try {
    const researchGroupMemberList = await ResearchGroupMembersService.getResearchGroupMembersList();

    res.status(200).json({
      data: researchGroupMemberList,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchGroupMemberList.controller][getResearchGroupMemberss][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取所有课题组成员列表失败！'
    });
  }
};

/**
 * Get researchGroupMembers record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getResearchGroupMembersById: RequestHandler = async (req: IGetResearchGroupMembersByIdReq, res: Response) => {
  try {
    const researchGroupMembers = await ResearchGroupMembersService.getResearchGroupMembersById(req.params.id);

    res.status(200).json({
      data: researchGroupMembers,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchGroupMemberList.controller][getResearchGroupMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取课题组成员列表失败！'
    });
  }
};

/**
 * Get researchGroupMembers record based on group id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getResearchGroupMembersByGroupId: RequestHandler = async (req: IGetResearchGroupMembersByGropuIdReq, res: Response) => {
    try {
      const researchGroupMembers = await ResearchGroupMembersService.getResearchGroupMembersByGroupId(req.params.researchGroupId);
  
      res.status(200).json({
        data: researchGroupMembers,
        code: 2000,
      })
    } catch (error) {
      console.error('[researchGroupMemberList.controller][getResearchGroupMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
      res.status(500).json({
        message: '获取所有课题组成员列表失败！'
      });
    }
  };

/**
 * Inserts a new researchGroupMembers record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addResearchGroupMembers: RequestHandler = async (req: IAddResearchGroupMembersReq, res: Response) => {
  try {
    const result = await ResearchGroupMembersService.insertResearchGroupMembers(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchGroupMemberList.controller][addResearchGroupMembers][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '添加课题组成员失败！'
    });
  }
};

/**
 * deletes a researchGroupMembers
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteResearchGroupMembersByGroupId: RequestHandler = async (req: IDeleteResearchGroupMembersReq, res: Response) => {
  try {
    const result = await ResearchGroupMembersService.deleteResearchGroupMembers(req.params.researchGroupId);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchGroupMemberList.controller][deleteResearchGroupMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '删除课题组成员信息失败！'
    });
  }
};